/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.huawei.spare.part.replace.views.replace.upgrade;

import com.huawei.spare.part.replace.framework.SmartContentPresenter;
import com.huawei.spare.part.replace.framework.SmartDataBus;
import com.huawei.spare.part.replace.models.Configs;
import com.huawei.spare.part.replace.models.PerformTask;
import com.huawei.spare.part.replace.models.Replacement;
import com.huawei.spare.part.replace.models.UpgradeTask;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ProgressBarTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.StringConverter;

/**
 * @author Woo
 */
public class UpgradePresenter extends SmartContentPresenter {

  private Disposable subscription;

  @FXML TableView upgradeTable;
  @FXML TableColumn<UpgradeTask, String> stepColumn;
  @FXML TableColumn<UpgradeTask, String> operationColumn;
  @FXML TableColumn<UpgradeTask, Double> progressColumn;
  @FXML TableColumn<UpgradeTask, UpgradeTask.Status> statusColumn;

  private Replacement.Slot slot;
  private Configs configs;

  @Override
  public void initialize() {

    initialTable();

    Platform.runLater(() -> {
      initDebugReplacement(); // TODO debug purpose

      configs = SmartDataBus.get().getConfigs();
      slot = SmartDataBus.get().replacement().getSlot();
    });
  }

  /**
   * 初始化表格
   */
  private void initialTable() {
    stepColumn.setCellValueFactory(new PropertyValueFactory("step"));
    operationColumn.setCellValueFactory(new PropertyValueFactory("operation"));
    progressColumn.setCellValueFactory(new PropertyValueFactory("progress"));
    statusColumn.setCellValueFactory(new PropertyValueFactory("status"));

    stepColumn.setCellFactory(TextFieldTableCell.<UpgradeTask>forTableColumn());
    operationColumn.setCellFactory(TextFieldTableCell.<UpgradeTask>forTableColumn());
    progressColumn.setCellFactory(ProgressBarTableCell.<UpgradeTask>forTableColumn());
    statusColumn.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<UpgradeTask.Status>() {
      @Override
      public String toString(UpgradeTask.Status status) {
        return status != null ? getString(status.getDisplay()) : "";
      }

      @Override
      public UpgradeTask.Status fromString(String string) {
        return null;
      }
    }));

    ObservableList<UpgradeTask> tasks = FXCollections.observableArrayList();
    tasks.add(new UpgradeTask("1", getString("upgrade.step.1"), 0D, UpgradeTask.Status.Pending));
    tasks.add(new UpgradeTask("2", getString("upgrade.step.2"), 0D, UpgradeTask.Status.Pending));
    tasks.add(new UpgradeTask("3", getString("upgrade.step.3"), 0D, UpgradeTask.Status.Pending));
    tasks.add(new UpgradeTask("4", getString("upgrade.step.4"), 0D, UpgradeTask.Status.Pending));
    tasks.add(new UpgradeTask("5", getString("upgrade.step.5"), 0D, UpgradeTask.Status.Pending));
    upgradeTable.setItems(tasks);

  }
}
